package com.xinghou.platform.server.api.controller;

import com.xinghou.platform.entity.req.FavoriteReq;
import com.xinghou.platform.entity.resp.ShareInfoResp;
import com.xinghou.platform.entity.resp.SubjectResp;
import com.xinghou.platform.server.api.bussiness.FavoriteCardService;
import com.xinghou.platform.server.api.bussiness.FavoriteSubjectService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by qingshengzheng on 16/6/23.
 */
@Controller
public class FavoriteController extends BaseControl {

    @Resource
    public FavoriteSubjectService subjectService;
    @Resource
    public FavoriteCardService cardService;

    /**
     * 新增收藏卡片
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addFavoriteCard", method = {RequestMethod.POST})
    public String addFavoriteCard( @RequestBody FavoriteReq favoriteReq,
                                    HttpServletRequest request, HttpServletResponse response) {
        cardService.add(favoriteReq.toFavoriteCard());
        return response(null);
    }
    /**
     * 新增话题收藏
     * @param favoriteReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addFavoriteSubject", method = {RequestMethod.POST})
    public String addFavoriteSubject( @RequestBody FavoriteReq favoriteReq,
                                    HttpServletRequest request, HttpServletResponse response) {
        subjectService.add(favoriteReq.toFavoriteSubject());
        return response(null);
    }


    /**
     * 删除卡片收藏
     * @param favoriteReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delFavoriteCard", method = {RequestMethod.POST})
    public String delFavoriteCard( @RequestBody FavoriteReq favoriteReq,
                                     HttpServletRequest request, HttpServletResponse response) {

        cardService.del(favoriteReq.getUserid(), Long.parseLong(favoriteReq.getCardid()));
        return response(null);
    }

    /**
     * 删除话题收藏
     * @param favoriteReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delFavoriteSubject", method = {RequestMethod.POST})
    public String delFavoriteSubject( @RequestBody FavoriteReq favoriteReq,
                                  HttpServletRequest request, HttpServletResponse response) {
        subjectService.del(favoriteReq.getUserid(),Long.parseLong(favoriteReq.getSubjectid()));
        return response(null);
    }

    @ResponseBody
    @RequestMapping(value = "/getFavoriteCards", method = {RequestMethod.POST})
    public String getFavoriteCards( @RequestBody FavoriteReq favoriteReq,
                      HttpServletRequest request, HttpServletResponse response) {
       List<ShareInfoResp> list= cardService.getFavoriteCards(favoriteReq.getUserid(), Integer.parseInt(favoriteReq.pageno), Integer.parseInt(favoriteReq.pagesize));
        return response(list);
    }

    @ResponseBody
    @RequestMapping(value = "/getFavoriteSubjects", method = {RequestMethod.POST})
    public String getFavoriteSubjects( @RequestBody FavoriteReq favoriteReq,
                                   HttpServletRequest request, HttpServletResponse response) {
        List<SubjectResp> list= subjectService.getFavoriteSubjectList(favoriteReq.getUserid(), Integer.parseInt(favoriteReq.getPageno()), Integer.parseInt(favoriteReq.pagesize));
        return response(list);
    }


}
